//导入axios
import axios from 'axios';

//声明变量,保存axios的对象
const request = axios.create({
  baseURL:'http://localhost:8083'
})

// Add a request interceptor  设置请求拦截
request.interceptors.request.use(function (config) {
  // Do something before request is sent
  config.headers['content-type']='application/json;charset=utf-8' //application/json
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor 设置响应拦截
request.interceptors.response.use(function (response) {
  // Do something with response data  返回数据之前处理返回的结果
  let res = response.data; //response表示返回结果,response.data表示后端接口返回的数据
  //判断返回的数据类型
  if(typeof(res) === 'string') //typeof数据类型
    res=res?JSON.parse(res):res; //JSON.parse将json串解析为json对象
  return res;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});

//导出对象才能使用
export default request;
